package org.montecristo.posmobile.rastreo;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;

import android.location.LocationManager;
import android.location.GpsStatus.NmeaListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.Vibrator;
import android.widget.Toast;
import org.montecristo.posmobile.R;

public class Servicio extends Service {
	
	private LocationManager lm;
	private Handler mHandler = new Handler();
	private Localizacion lc;
	private NotificationManager mNM;
   	private long time=0;
	 
	@Override
	public IBinder onBind(Intent intent) {		
		return null;
	}
		
	
	@Override
    public int onStartCommand(Intent intent, int flags,int startId){     								  							
		PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);	
		PowerManager.WakeLock wl= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"hola");
		wl.acquire();
		lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
		Localizacion lc=new Localizacion(this, 101,lm,this);	
		lm.requestLocationUpdates(getBestProvider(),0, 0, lc);
		wl.release();	
		return START_NOT_STICKY;  
			
		
    }
	
	PowerManager pm;
    PowerManager.WakeLock wl;
	PendingIntent pend;


	
	@Override
	public void onDestroy() {		
		super.onDestroy();
    }	
	
		
   private void showNotification(String Cadena){
        CharSequence text = Cadena;        
        Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());      
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,Inicio.class), 0);  
        notification.setLatestEventInfo(this, "Servicio Iniciado", text, contentIntent);        
        notification.flags=Notification.FLAG_ONGOING_EVENT;        
        startForeground(1, notification);
    }
		
   
	public String getBestProvider(){    	
	       	Criteria c = new Criteria();
	    	c.setAccuracy(Criteria.ACCURACY_FINE);	    
	    	c.setAltitudeRequired(false);
	    	c.setBearingRequired(false);
	    	c.setCostAllowed(true);
	    	c.setPowerRequirement(Criteria.POWER_HIGH);
	    	String bestProvider = lm.getBestProvider(c, true);	    	
	    	return bestProvider;
	   } 	 	
				
	
	Runnable r= new Runnable() {
		public void run() {			
				//wl.acquire();
			    //mHandler.postDelayed(r,300000);
				//if((System.currentTimeMillis()-time) > 600000){
					//wl.acquire();
					//time=System.currentTimeMillis();
					doTask();			     			    
				//}  	       
		}
	};
	
	
	private void doTask(){		
	   lm.requestLocationUpdates(getBestProvider(),0, 0, lc);	
	  
	}

	
	
}
